package com.lastproject.ejb.util;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.QuestionDTO;
import com.lastproject.ejb.entities.Question;
import com.lastproject.ejb.entities.Survey;

/**
 * Session Bean implementation class QuestionConversion
 */
@Stateless
@LocalBean
public class QuestionConversion {
	@PersistenceContext
	EntityManager em;
	
	@EJB
	SurveyConversion sconv;

	/**
	 * Default constructor.
	 */
	public QuestionConversion() {
		// TODO Auto-generated constructor stub
	}

	public QuestionDTO fromEntity(Question q) {
		QuestionDTO question = new QuestionDTO();
		question.setId(q.getId());
		question.setText(q.getText());
		question.setSurveyId(q.getSurvey().getId());

		return question;
	}

	public Question fromDTO(QuestionDTO q) {
		Question question = em.find(Question.class, q.getId());
		if (question == null) {
			question = new Question();
		}

		question.setText(q.getText());
		question.setSurvey(em.find(Survey.class, q.getSurveyId()));

		return question;
	}

}
